<template>
  <div class="school">
    <h2>学校名称：{{ name }}</h2>
    <h2>学校地址：{{ address }}</h2>
  </div>
</template>

<script>
import pubsub from "pubsub-js";
export default {
  name: "School",
  data() {
    return {
      name: "尚硅谷",
      address: "北京",
    };
  },
  mounted() {
    // console.log('School',this)
    /* this.$bus.$on('hello',(data)=>{
				console.log('我是School组件，收到了数据',data)
			}) */

    // pubsub插件， pubsub.subscribe订阅消息，参数为消息名称和回调函数（回调函数参数包含两项，第一个为消息名称，第二个为返回的数据）
    this.pubId = pubsub.subscribe("hello", (msgName, data) => {
      console.log(this);
      // console.log('有人发布了hello消息，hello消息的回调执行了',msgName,data)
    });
    this.pubId2 = pubsub.subscribe("userName", (msgName, userName) => {
      console.log("获取用户名为", userName);
    });
  },
  beforeDestroy() {
    // this.$bus.$off('hello')
    // 取消订阅;
    pubsub.unsubscribe(this.pubId);
    pubsub.unsubscribe(this.pubId2);
  },
};
</script>

<style scoped>
.school {
  background-color: skyblue;
  padding: 5px;
}
</style>